package com.hui.account.activity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.google.gson.Gson;
import com.hui.account.R;
import com.hui.account.adapter.RecordAdapter;
import com.hui.account.database.SQLManage;
import com.hui.account.entity.FileEntity;
import com.hui.account.entity.RecordEntity;
import com.hui.account.utils.ButtonUtils;
import com.hui.account.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 回收站
 */
public class RecoverActivity extends AppCompatActivity implements View.OnClickListener {

    Context context;

    ListView recordList;

    Button backBtn, clearBtn;

    RecordAdapter adapter;

    List<RecordEntity> data;

    SharedPreferences preferences; // preferences：共享参数，存储选中的欠款单信息

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        setContentView(R.layout.activity_recover);

        // 1、设置屏幕顶部字体颜色
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        // 2、初始化页面
        initView();

        // 3、preferences：共享参数初始化
        preferences = getSharedPreferences("shareParam", Context.MODE_PRIVATE);

        // 4、设置 ListView点击事件
        onClickRecordList();

        // 引入欠款记录适配器
        data = new ArrayList<>();
        adapter = new RecordAdapter(this, data);
        recordList.setAdapter(adapter);
    }

    /**
     * 初始化页面
     */
    private void initView() {
        backBtn = findViewById(R.id.recover_back);
        clearBtn = findViewById(R.id.recover_clear);
        recordList = findViewById(R.id.recover_records);
    }

    /**
     * Activity加载时执行
     */
    @Override
    protected void onResume() {
        super.onResume();

        loadRecordData();
    }

    /**
     * 加载执行逻辑删除的欠款记录
     */
    private void loadRecordData() {
        // 查询执行逻辑删除的欠款记录
        List<RecordEntity> list = SQLManage.getLogicDelRecords();
        if (list.size() > 0) {
            data.clear();
            data.addAll(list);
            adapter.notifyDataSetChanged();
        } else {
            data.clear();
            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 欠款单适配器点击事件
     */
    private void onClickRecordList() {
        recordList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (!ButtonUtils.isFastDoubleClick()) {
                    // 1、获取到当前记录
                    RecordEntity record = data.get(i);

                    // 2、将数据存入共享参数
                    SharedPreferences.Editor edit = preferences.edit();
                    edit.putString("selectRecord", new Gson().toJson(record));
                    edit.commit();

                    startActivity(new Intent(context, RecordRecoverActivity.class));
                }
            }
        });
    }

    /**
     * 清空回收站
     */
    private void recoverClear() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确定清空").setMessage("是否确认清空回收站，永久清空？").setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                // 1、查询执行逻辑删除的欠款记录
                List<RecordEntity> list = SQLManage.getLogicDelRecords();
                if (list.size() > 0) {
                    // 2、清除欠款单图片
                    List<String> images = list.stream().map(RecordEntity::getImageName).collect(Collectors.toList());
                    List<FileEntity> files = SQLManage.getFilesByName(images);
                    if (files.size() > 0) {
                        for (FileEntity file : files) {
                            SQLManage.delFile(file.getId());
                            ImageUtils.delDiskFile(file.getPath());
                        }
                    }

                    // 3、清除欠款记录
                    SQLManage.delRecords();

                    data.clear();
                    adapter.notifyDataSetChanged();
                }
            }
        });
        builder.create().show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.recover_back:
                finish();
                break;
            case R.id.recover_clear:
                if (!ButtonUtils.isFastDoubleClick())
                    recoverClear();

                break;
        }

    }

}